package com.song.controller;



import com.song.polo.*;
import com.song.service.impl.ExpressServiceImpl;

import com.song.utils.JsonUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("express")
@ResponseBody
public class ExpressController {
    private AjxaResultMessage msg;
    @Resource
    private ExpressServiceImpl expressService;

    /**
     * 查询所有快递信息
     *
     * @return
     */
    @RequestMapping(value = "/list/",method = RequestMethod.GET)
    public String selectAll(@RequestParam(value = "pageIndex",required = false)Integer pageIndex, @RequestParam(value = "pageSize",required = false)Integer pageSize) {
        //调用service
        List<Express> expresses = expressService.selectExpress(true, pageIndex, pageSize);
        for (Express express : expresses) {
            System.out.println(express);
        }
        //信息总数
        int total = expressService.selectExpress(false, 0, 0).size();
        ResultData<Express> data = new ResultData<>();
        data.setRows(expresses);
        data.setTotal(total);
        return JsonUtil.toJSON(data);
    }

    /**
     * 新添快递
     *
     *
     */
    @RequestMapping(method = RequestMethod.POST)
    public AjxaResultMessage<Express> insertExpress(Express express,HttpServletRequest request){
        System.out.println(express);
        Administrator admin = (Administrator) request.getSession().getAttribute("admin");
        express.setReceiverPhone(admin.getAdministratorPhone());
        Integer integer = expressService.insertExpress(express);
        if (integer == 1) {
            msg = new AjxaResultMessage(1,"添加成功");
        } else if (integer == -1) {
            msg = new AjxaResultMessage(-1,"快递单号已存在");
        } else {
            msg = new AjxaResultMessage(0,"添加失败");
        }
        return msg;
    }


    /**
     * 通过快递单号查询快递

     */
    @RequestMapping(value = "{number}",method = RequestMethod.GET)
    public AjxaResultMessage<Express> selectByNumber(@PathVariable("number") String expressNumber){
        Express express = expressService.selectByNumber(expressNumber);
        if (express == null){
            msg = new AjxaResultMessage(0,"改单号不存在");

        }else {
            msg = new AjxaResultMessage(1,"查询成功",null,express);
        }
        return msg;
    }

    /**
     * 修改快递信息

     */
    @RequestMapping(value = "{id}",method = RequestMethod.PUT)
    public AjxaResultMessage<Express> updateExpress(@PathVariable("id")Integer expressId, Express express,HttpServletRequest request){
       /* Administrator admin = (Administrator) request.getSession().getAttribute("admin");
        express.setReceiverPhone(admin.getAdministratorPhone());*/
        express.setExpressId(expressId);
        Integer integer = expressService.updateExpress(express);
        if (integer == 1){
            msg = new AjxaResultMessage(1,"修改成功");
        }else if (integer == -1){
            msg = new AjxaResultMessage(-1,"快递单号已存在");
        }else {
            msg = new AjxaResultMessage(0, "修改失败");
        }
        return msg;
    }


    /**
     * 删除快递

     * @return
     */
    @RequestMapping(value = "{id}",method = RequestMethod.DELETE)
    public AjxaResultMessage<Express> deleteExpress(@PathVariable("id") Integer expressId){

        Integer integer = expressService.deleteExpress(expressId);
        if (integer == 1){
            msg = new AjxaResultMessage(1,"删除成功");
        }else {
            msg = new AjxaResultMessage(0, "删除失败");
        }
        return msg;
    }


    /**
     * 控制台中的数据

     * @return 给前端的用户数据
     */
    @ResponseBody
    @RequestMapping(value = "console",method = RequestMethod.GET)
    public AjxaResultMessage<Express> console(){
        Map<String, Object> expressMap = expressService.expressConsole();
        if (expressMap.size() > 0){
            msg = new AjxaResultMessage(1,null,expressMap);
        }else {
            msg = new AjxaResultMessage(-1,null);
        }
        return msg;
    }



}
